package com.igeek.compus.test.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.igeek.compus.test.menu.entity.Menu;
import com.igeek.compus.test.product.dao.ProductDao;
import com.igeek.compus.test.product.entity.Product;
import com.igeek.compus.test.product.service.ProductService;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Override
    public List<Product> getProductListByPage(Product product) {
        PageHelper.startPage(product.getPage(), product.getLimit());
        return productDao.getProductListByPage(product);
    }
    @Override
    public int getProductListCount(Product product) {
        return productDao.getProductListCount(product);
    }

    @Override
    public Product getProductInfoById(Integer id){
        return productDao.getProductInfoById(id);
    }

    @Override
    public int deleteProductById(Product product){
        return productDao.deleteProductById(product);
    }

    @Override
    public int deleteProductList(List<String> productIdList){
        return productDao.deleteProductList(productIdList);
    }

    @Override
    public int addProduct(Product product) {
        return productDao.addProduct(product);
    }

    @Override
    public int updateProduct(Product product) {
        return productDao.updateProduct(product);
    }

    @Override
    public HSSFWorkbook productExport(String[] titles, List<Product> productList) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();

//        CellStyle style = wb.createCellStyle();
////        style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
//        style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.GREEN.getIndex());
//        style.setBorderBottom(BorderStyle.THIN);
//        style.setBorderLeft(BorderStyle.THIN);
//        style.setBorderRight(BorderStyle.THIN);
//        style.setBorderTop(BorderStyle.THIN);

        HSSFRow head = sheet.createRow(0);
        for(int i = 0; i < titles.length; i++) {
            HSSFCell cell = head.createCell(i);
            cell.setCellValue(titles[i]);
        }

        for(int i = 0; i < productList.size(); i++) {
            HSSFRow row = sheet.createRow(i + 2);
            row.setHeight((short) 350);
            Product product = productList.get(i);

            HSSFCell cell1 = row.createCell(0);
//            cell1.setCellStyle(style);
            cell1.setCellValue(product.getNickName());

            HSSFCell cell2 = row.createCell(1);
            cell2.setCellValue(product.getProductName());

            HSSFCell cell3 = row.createCell(2);
            cell3.setCellValue(product.getDescription());

            HSSFCell cell4 = row.createCell(3);
            cell4.setCellValue(product.getPrice().doubleValue());

            HSSFCell cell5 = row.createCell(4);
            cell4.setCellValue(product.getCreateTime());
        }

        return wb;
    }

}
